package com.tanhua.server.service;

import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.ContractVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class IMService {
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;
    /**
     * 接口名称：联系人添加 (添加友好)
     * 接口路径：POST/messages/contacts
     */
    public ResponseEntity<Object> addContacts(Long friendId) {
        //0. 获取登陆用户id
        Long userId = UserHolder.getUserId();
        //1. 保存好友关系到mongo
        friendApi.addContacts(userId,friendId);
        //2. 注册好友关系到环信
        huanXinTemplate.contactUsers(userId,friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：联系人列表
     */
    public ResponseEntity<Object> contactsList(Integer page, Integer pagesize, String keyword) {
        //1. 调用Api服务查询用户好友列表
        PageResult pageResult = friendApi.findByPage(page,pagesize,UserHolder.getUserId());
        //2. 获取分页数据
        List<Friend> list = (List<Friend>) pageResult.getItems();
        //3. 定义返回的vo集合
        List<ContractVo> voList = new ArrayList<>();
        //4. 封装vo集合
        if (!CollectionUtils.isEmpty(list)) {
            for (Friend friend : list) {
                ContractVo vo = new ContractVo();
                vo.setId(friend.getUserId());
                vo.setUserId(friend.getFriendId().toString());
                // 根据好友id查询
                UserInfo userInfo = userInfoApi.findById(friend.getFriendId());
                BeanUtils.copyProperties(userInfo,vo);
                // 添加到集合
                voList.add(vo);
            }
        }
        //5. 设置vo集合到分页对象中并返回
        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 点赞列表
     */
    public ResponseEntity<Object> likeLIst(Integer page, Integer pagesize) {
        Integer commentType=1;
        PageResult pageResult = this.queryList(commentType, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 评论列表
     */
    public ResponseEntity<Object> commentLIst(Integer page, Integer pagesize) {
        Integer commentType=2;
        PageResult pageResult = this.queryList(commentType, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     *喜欢列表
     */
    public ResponseEntity<Object> loveLIst(Integer page, Integer pagesize) {
        Integer commentType=3;
        PageResult pageResult = this.queryList(commentType, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 列表抽取
     */
    public PageResult queryList(Integer commentType,Integer page,Integer pagesize){
        Long userId = UserHolder.getUserId();
        PageResult pageResult = commentApi.queryByUserIdAndType(userId,commentType,page,pagesize);
        List<Map> list = (List<Map>) pageResult.getItems();
        List<CommentVo> voList=new ArrayList<>();
        if (list!=null){
            for (Map map : list) {
                Comment groups = (Comment) map.get("groups");
                CommentVo vo = new CommentVo();
                UserInfo userInfo = userInfoApi.findById(groups.getUserId());
                if (userInfo!=null) {
                    BeanUtils.copyProperties(userInfo, vo);
                    vo.setContent("");
                    vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(groups.getCreated()));
                    vo.setLikeCount(0);
                    vo.setHasLiked(0);
                    voList.add(vo);
                }
            }
        }
        pageResult.setItems(voList);
        return pageResult;
    }
}


















